layui.config({
    base: '/layui/extend/'
}).use(['table', 'element', 'form', 'dtree', 'tablePlug'], function () {
    var table = layui.table;
    var $ = layui.$;
    var form = layui.form;
    var dtree = layui.dtree;
    var tablePlug = layui.tablePlug;

    tablePlug.smartReload.enable(true);

    //文档列表展示
    var tableIns = table.render({
        elem: '#document-table',
        url: '/document/allList',
        toolbar: '#toolbar',
        cellMinWidth : 95,
        smartReloadModel: true,
        page: true,
        id: "documentListTable",
        limits : [5,10,15,20,25],
        limit : 5,
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: "id", title:'编号',fixed:"true", width:80},
            {field: 'projectName', title: '所属项目', minWidth:50, align:"center"},
            {field: 'fileName', title: '文件名', align:'center'},
            {field: 'fileType', title: '文件类型', minWidth:100, align:'center',width: 90},
            {field: 'uploadPerson', title: '上传者', align:'center', width: 80},
            {field: 'uploadTime', title: '上传时间', align:'center',minWidth:150},
            {title: '操作', fixed: 'right', align: 'center', toolbar: '#column-toolbar'}
        ]]
    });


    //条件搜索按钮
    $(".search_btn").on("click", function (){
        console.log($("input[name='documentname']").val());
        table.reload("documentListTable", {
            page: {
                curr:1
            },
            where:{
                documentName: $("input[name='documentname']").val(),  //用户名
            }
        })
    })


    // 行点击事件 重置密码
    table.on('tool', function (obj) {
        var data = obj.data;
        var event = obj.event;
        if (event === 'view') {
            view(data.id);
        } else if(event === 'download') {
            download(data.id);
        } else if(event === 'check') {
            check(data.id);
        }
    });


    function view(id) {
        var url = "/document/getUrl"
        if(id) {
            url = url+"?id="+id;
        }
        $.post(url,{id: id}, function (data) {
            data = eval("("+data+")")
            console.log(data);
            if(!("url" in data)) {
                window.open("../abnormalPage/404.html")
            }
            else if(data.code==200) {
                window.open(data.url);
            } else {
                window.open("../abnormalPage/404.html")
            }
        });

    }


    function download(id) {
        var url = "/document/getUrl"
        if(id) {
            url = url+"?id="+id;
        }
        $.post(url,{id: id}, function (data) {
            data = eval("("+data+")")
            console.log(data);
            if(!("url" in data)) {
                var link = document.createElement('a');
                link.href="../abnormalPage/404.html";
                link.click()
            }
            else if(data.code==200) {
                var link = document.createElement('a');
                link.href = data.url;
                link.download = data.url.substring(28,data.url.lastIndexOf("."));
                console.log(link.download);
                link.click();
            } else {
                var link = document.createElement('a');
                link.href="../abnormalPage/404.html";
                link.click()
            }
        });
    }

    function check(id) {
        var url = "/document/toViewCheckResult"
        if(id) {
            url = url+"?id="+id;
        }
        layer.open({
            content: url,
            title: "审核结果",
            area: ['40%', '65%'],
            type: 2,
            maxmin: true,
            shadeClose: true,
            end: function () {
                table.reload('document-table');
            }
        });
    }

    form.on('submit(search)', function (form) {
        table.reload('document-table', {
            where: form.field
        });
        return false;
    });

});